package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.*;
import com.example.springboot.mapper.OrdersMapper;
import com.example.springboot.service.ICartService;
import com.example.springboot.service.IGoodsService;
import com.example.springboot.service.IOrderGoodsService;
import com.example.springboot.service.IOrdersService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgc
 * @since 2023-09-16
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private IOrdersService ordersService;

    @Resource
    private IOrderGoodsService orderGoodsService;

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private ICartService cartService;

    @Resource
    private IGoodsService goodsService;

    //新增和修改
    @PostMapping
    public Result save(@RequestBody Orders orders) {  //@RequestBody接收前端数据
        // 编写订单
        if (orders.getId() == null) {
            Date date = new Date();
            orders.setTime(DateUtil.formatDateTime(date));
            if(orders.getNo() == null){
                orders.setNo(DateUtil.format(date, "yyyyMMdd") + System.currentTimeMillis());
            }
            orders.setUserId(TokenUtils.getCurrentUser().getId());

            ordersService.save(orders);

            List<Cart> carts = orders.getCarts();
            for (Cart cart : carts) {
                OrderGoods orderGoods = new OrderGoods();
                orderGoods.setGoodsId(cart.getGoodsId());
                orderGoods.setNum(cart.getNum());
                orderGoods.setOrderId(orders.getId());
                orderGoodsService.save(orderGoods);

                // 结算订单后删除购物车数据
                cartService.removeById(cart.getId());
            }
        } else {
            ordersService.updateById(orders);
        }
        return Result.success();
    }

    //查询所有数据
    @GetMapping()
    public Result findAll() {
        return Result.success(ordersService.list());
    }

    //查询单个数据
    @GetMapping("/getGoodsById/{id}")
    public Result getGoodsById(@PathVariable Integer id) {
        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", id);

        ArrayList<Goods> goodsList = new ArrayList<>();
        List<OrderGoods> orderGoodsList = orderGoodsService.list(queryWrapper);
        for (OrderGoods orderGoods : orderGoodsList) {
            Integer goodsId = orderGoods.getGoodsId();
            Goods goods = goodsService.getById(goodsId);
            goods.setNum(orderGoods.getNum());
            goodsList.add(goods);
        }
        return Result.success(goodsList);
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(ordersService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(ordersService.removeByIds(ids));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(ordersService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
//        if (!"".equals(name)) {
//        queryWrapper.like("name", name);
//        }
//        return Result.success(ordersPlusService.page(new Page<>(pageNum, pageSize), queryWrapper));
        User currentUser = TokenUtils.getCurrentUser();
        return Result.success(ordersMapper.ordersPage(new Page<>(pageNum, pageSize), name, currentUser.getUserAdmin(),
                currentUser.getId()));
    }
}



